<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/2/24
 * Time: 16:17
 */
namespace Admin\Controller;
use Think\Controller;
use Think\Page;
use Think\Think;
use Common\Model\OptionModel;

class FuturesFundSummaryController extends AdminController{
    /**
     * 资金汇总列表
     * */
    public function index(){
    	$qqlcd_Model = new OptionModel('order');				  //恒指订单
    	$ouyuan_model = new OptionModel('euro_order');		//欧元订单
      $gold_model = new OptionModel('goldorder');			//伦敦金订单
      $dollar_model = new OptionModel('dollarorder');			//美元订单
	     $dax_model = new OptionModel('daxorder');         //德指订单
    	 $bite_model = new OptionModel('biteorder');       //比特比订单
      //判断是否有用户查询
        $uid = $_GET['id'];						    //获取用户id
      	$rec['a.is_pay'] = 1;
      	$cash['a.is_pay'] = array('neq',2);
        if(!$_GET['id']){
          $rec['b.maker_id'] = array('neq',0);
          $cash['b.maker_id'] = array('neq',0);
          $cms['b.maker_id'] = array('neq',0);
          $odr['b.maker_id'] = array('neq',0);
          $sql['maker_id'] = array('neq',0);
          $member = M('follow')->where($sql)->select();
          foreach($member as $k=>$v){
            $routes[$k] = $v['id'];
          }
          $mongo_sql['user_id'] = array('in',$routes);
        }else{
          $mongo_sql['user_id'] = $uid;			//订单查询条件
          $odr['a.user_id'] = $uid;					  //螺纹钢
          $rec['a.user_id'] = $uid;					  //充值查询条件
          $cash['a.user_id'] = $uid;          //提现查询条件
          $cms['a.commission_user_id'] = $uid;		//佣金查询条件
          $uname = M('follow')->where('id='.$uid)->field('nickname,money')->find();		//获取用户名
        }
        //时间段搜索
        $stime = strtotime($_REQUEST['start_time']);
        if(empty($stime)){
          $stime=strtotime(date('Y-m-d',strtotime('-7 days')));
        }
        $etime = strtotime($_REQUEST['end_time']);
        if(empty($etime)){
          $etime=time();
        }

        if($stime){
          $cms['a.create_time'] = array('egt',$stime);//佣金
          $odr['a.create_time']  = array('egt',$stime);//螺纹钢
          $rec['a.create_time'] = array('egt',$stime);//充值时间
          $cash['a.add_time'] = array('egt',$stime);	//提现时间
          $mongo_sql['start_time'] = array('$gte'=>$stime);	//下单时间
        }
        if($etime){
          $end_time = $etime+86400;
          $cms['_string'] = 'a.create_time <='.$end_time;//佣金
          $rec['_string'] = 'a.create_time <='.$end_time;//充值时间
          $cash['_string'] = 'a.add_time <='.$end_time;	//提现时间
          $odr['_string'] = 'a.create_time <='.$end_time;       //螺纹钢
          if(empty($stime)){
            $mongo_sql['start_time'] = array();
          }
          $mongo_sql['start_time'] += array('$lte'=>$end_time);	//下单时间
        }
      	//欧元下单数据
      	$oy_order = $ouyuan_model->where($mongo_sql)->field('invest,profit')->select();
        $profit['oy_order']['title'] = '欧元订单';
        foreach($oy_order as $v){
      		$profit['oy_order']['invest'] += $v['invest'];
          if($v['profit'] > 0){
        		$profit['oy_order']['profit'] += $v['profit']-$v['invest'];
        	}else{
        		$profit['oy_order']['loss'] += $v['profit'];
        	}
      	}
        //$profit['hz_order']['fee'] = $profit['hz_order']['invest']*0.02;
		    $profit['hz_order']['fee'] = 0;
      	$profit['oy_order']['pal'] = -$profit['oy_order']['loss']-$profit['oy_order']['profit']-$profit['oy_order']['fee'];//计算公司盈亏
      	//恒指下单数据
      	$order = $qqlcd_Model->where($mongo_sql)->field('invest,profit')->select();
        $profit['hz_order']['title'] = '恒指订单';
        foreach($order as $v){
      		$profit['hz_order']['invest'] += $v['invest'];
          if($v['profit'] > 0){
        		$profit['hz_order']['profit'] += $v['profit']-$v['invest'];
        	}else{
        		$profit['hz_order']['loss'] += $v['profit'];
        	}
      	}
      	//$profit['hz_order']['fee'] = $profit['hz_order']['invest']*0.02;
		    $profit['hz_order']['fee'] = 0;
      	$profit['hz_order']['pal'] = -$profit['hz_order']['loss']-$profit['hz_order']['profit']-$profit['hz_order']['fee'];//计算公司盈亏
        //伦敦金下单数据
        $gold_order = $gold_model->where($mongo_sql)->field('invest,profit')->select();
        $profit['gold_order']['title'] = '伦敦金订单';
        foreach($gold_order as $v){
          $profit['gold_order']['invest'] += $v['invest'];
          if($v['profit'] > 0){
            $profit['gold_order']['profit'] += $v['profit']-$v['invest'];
          }else{
            $profit['gold_order']['loss'] += $v['profit'];
          }
        }
        //$profit['gold_order']['fee'] = $profit['gold_order']['invest']*0.02;
		    $profit['gold_order']['fee'] = 0;
        $profit['gold_order']['pal'] = -$profit['gold_order']['loss']-$profit['gold_order']['profit']-$profit['gold_order']['fee'];//计算公司盈亏
        //美元下单数据
        $dollar_order = $dollar_model->where($mongo_sql)->field('invest,profit')->select();
        $profit['dollarorder']['title'] = '美元订单';
        foreach($dollar_order as $v){
          $profit['dollarorder']['invest'] += $v['invest'];
          if($v['profit'] > 0){
            $profit['dollarorder']['profit'] += $v['profit']-$v['invest'];
          }else{
            $profit['dollarorder']['loss'] += $v['profit'];
          }
        }
        //$profit['dollarorder']['fee'] = $profit['dollarorder']['invest']*0.02;
		    $profit['dollarorder']['fee'] = 0;
        $profit['dollarorder']['pal'] = -$profit['dollarorder']['loss']-$profit['dollarorder']['profit']-$profit['dollarorder']['fee'];//计算公司盈亏

		    //德指下单数据
        $dax_order = $dax_model->where($mongo_sql)->field('invest,profit')->select();
        $profit['daxorder']['title'] = '德指订单';
        foreach($dax_order as $v){
          $profit['daxorder']['invest'] += $v['invest'];
          if($v['profit'] > 0){
            $profit['daxorder']['profit'] += $v['profit']-$v['invest'];
          }else{
            $profit['daxorder']['loss'] += $v['profit'];
          }
        }
        //$profit['daxorder']['fee'] = $profit['daxorder']['invest']*0.02;
		    $profit['daxorder']['fee'] = 0;
        $profit['daxorder']['pal'] = -$profit['daxorder']['loss']-$profit['daxorder']['profit']-$profit['daxorder']['fee'];//计算公司盈亏

        //比特币下单数据
        $bite_order = $bite_model->where($mongo_sql)->field('invest,profit')->select();
        $profit['biteorder']['title'] = '比特币订单';
        foreach($bite_order as $v){
          $profit['biteorder']['invest'] += $v['invest'];
          if($v['profit'] > 0){
            $profit['biteorder']['profit'] += $v['profit']-$v['invest'];
          }else{
            $profit['biteorder']['loss'] += $v['profit'];
          }
        }
        //$profit['biteorder']['fee'] = $profit['biteorder']['invest']*0.02;
		    $profit['biteorder']['fee'] = 0;
        $profit['biteorder']['pal'] = -$profit['biteorder']['loss']-$profit['biteorder']['profit']-$profit['biteorder']['fee'];//计算公司盈亏

		//螺纹钢订单数据
      	$order = M('Futures_order')->table('wp_futures_order as a')->join('wp_follow as b on a.user_id = b.id')->where($odr)->field('a.id,a.total_price,a.profit')->select();
        $profit['order']['title'] = '螺纹钢订单';
        foreach($order as $v){
      		$profit['order']['invest'] += $v['total_price'];
      		$income = $v['profit']-$v['total_price'];
          if($income > 0){
        		$profit['order']['profit'] += $income;
        	}else{
        		$profit['order']['loss'] += $income;
        	}
      	}
        $profit['order']['fee'] = $profit['order']['invest']*0.02;
		$profit['order']['fee'] = 0;
      	$profit['order']['pal'] = -$profit['order']['loss']-$profit['order']['profit']-$profit['order']['fee'];//计算公司盈亏

      	//佣金数据
      	$commission = M('Futures_commission')->table('wp_futures_commission as a')->join('wp_follow as b on a.commission_user_id = b.id')->where($cms)->field('a.commission,a.status')->select();
        $com_data['already']['title'] =  '已分配';
        $com_data['not']['title'] =  '未分配';
        foreach($commission as $v){
      		if($v['status'] == 0){
      				$com_data['not']['commission'] += $v['commission'];
      		}elseif($v['status'] == 1){
      		    $com_data['already']['commission'] += $v['commission'];
      		}
      	}

      	//充值数据
      	$recharge = M('Futures_recharge')->table('wp_futures_recharge as a')->join('wp_follow as b on a.user_id = b.id')->where($rec)->field('a.money')->select();
        $gold['rec']['title'] = '充值';
        foreach($recharge as $v){
      		$gold['rec']['money'] +=	$v['money'];
      		$gold['rec']['fee'] += $v['money']*0.009;//充值手续费
      	}

      	//提现数据
      	$withdrawals = M('futures_withdrawals')->table('wp_futures_withdrawals as a')->join('wp_follow as b on a.user_id = b.id')->where($cash)->field('a.money,a.handling_fee')->select();
        $gold['cash']['title'] = '提现';
        foreach($withdrawals as $v){
      		$gold['cash']['money'] += $v['money'];
      		$gold['cash']['fee'] += $v['handling_fee'];//提现手续费
      	}

  	    //判断数据是否为空
        foreach($profit as $k => $v){
					$profit[$k]['invest'] = !empty($v['invest']) ? round($v['invest'],2) : 0;
					$profit[$k]['profit'] = !empty($v['profit']) ? round($v['profit'],2) : 0;
					$profit[$k]['loss'] = !empty($v['loss']) ? -round($v['loss'],2) : 0;
					$profit[$k]['pal'] = !empty($v['pal']) ? round($v['pal'],2) : 0;
          $profit[$k]['fee'] = !empty($v['fee']) ? round($v['fee'],2) : 0;
				}

        foreach ($com_data as $k => $v) {
					$com_data[$k]['commission'] = !empty($v['commission']) ? round($v['commission'],2) : 0;
				}

        foreach($gold as $k => $v){
					$gold[$k]['money'] = !empty($v['money']) ? round($v['money'],2) : 0;
					$gold[$k]['fee'] = !empty($v['fee']) ? round($v['fee'],2) : 0;
				}
        $this->assign('start_time',$stime);
        $this->assign('end_time',$etime);
  	    $this->assign('uname',$uname);
      	$this->assign('com_data',$com_data);
      	$this->assign('profit',$profit);
      	$this->assign('money',$money);
      	$this->assign('gold',$gold);
        $this->display('index');
    }
}
